package com.platform.server.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.platform.server.common.Constant;
import com.platform.server.controller.BaseController;
import com.platform.server.response.pojo.ReturnData;
import com.platform.server.util.StringTools;
import com.platform.server.util.UUIDUtil;
@Controller
@RequestMapping("upload")
public class ImageController extends BaseController{

	     @ResponseBody
	    @RequestMapping(value = "/photoImg",method = RequestMethod.POST)
	    public Object pCPhotoUpload(String type, String needsThumb, HttpServletRequest request)throws Exception{
	        try {
	            String  dir= "images";         
	            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
	            StringBuffer pictureUrls =new StringBuffer();
	            List<String> imagePath = new ArrayList<>();
	            if (multipartResolver.isMultipart(request)) {
	                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
	                Iterator<String> iter = multiRequest.getFileNames();
	                if(iter.hasNext()){
	                    while (iter.hasNext()) {
	                        MultipartFile file = multiRequest.getFile((String) iter.next());
	                        //文件名称
	                        String fileName = file.getOriginalFilename();
	                        if (!fileName.equals("")) {
									/*String path1 = Thread.currentThread()
											.getContextClassLoader().getResource("").getPath() + "dowload" + File.separator;*/
	                            //获得文件后缀名称
	                            String prefix =fileName.substring(fileName.lastIndexOf(".")+1);
	                            //图片文件放的位置文件夹目录
	                            String path = "";
	                           /* if(prefix.equalsIgnoreCase("png") || prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("bmp")){*/
	                                // 项目在容器中实际发布运行的根路径
	                                String realPath =  request.getSession().getServletContext().getRealPath("/");
	                                String path2 = realPath.substring(0,realPath.lastIndexOf(System.getProperty("file.separator")));
	                                String p = realPath.substring(0, (path2.lastIndexOf(System.getProperty("file.separator"))))+System.getProperty("file.separator")+"zycxFile"+System.getProperty("file.separator")+dir;
	                                // 自定义的文件名称
	                                String trueFileName= UUIDUtil.getOrderSnByTime18()+"."+prefix;
	                                // 设置存放图片文件的路径
	                                path=p+System.getProperty("file.separator")+trueFileName;
	                                //转为标准文件盘符格式
	                                String acesP = "http://"+ Constant.SERVER_NAME+":"+request.getServerPort()+"//"+"zycxFile"+"//"+dir+"//"+trueFileName;
	                                final File localFile = new File(path);
	                                //将文件复制到指定文件夹下面
	                                try {
	                                    if(!localFile.exists()){
	                                        localFile.mkdirs();
	                                    }
	                                    file.transferTo(localFile);
	                                } catch (Exception e) {
	                                }
	                                imagePath.add(path);
	                                StringBuffer sb = new StringBuffer();
	                                sb.append(acesP);
	                                sb.append(",");
	                                pictureUrls.append(sb);
	                            /*}else{
	                                return new ReturnData(702,"系统暂不支持此格式图片");
	                            }*/
	                        }
	                    }
	                    String purl = "";
	                    if(StringTools.isEmpty(pictureUrls.toString())){
	                        return new ReturnData(703,"没有图片路径");
	                    }else {
	                        Map<String,String> retMap =new HashMap<>();
	                        purl = pictureUrls.substring(0, pictureUrls.length() - 1);
	                        retMap.put("purl", purl);
	        /*                    
	                            是否要压缩图片
	                            
	                        if(StringTools.numIsGT0(needsThumb)&&Integer.parseInt(needsThumb)==1) {
	                            StringBuffer thumbBuff = new StringBuffer();
	                            for (String img : imagePath) {
	                                String thumb = ImageCompressUtil.saveMinPhoto(img, Constant.IMAGECOMPRESS_SMALLICON, Constant.IMAGECOMPRESS_COMBASE, Constant.IMAGECOMPRESS_SCALE);
	                                thumb = thumb.substring(thumb.lastIndexOf(System.getProperty("file.separator")));
	                                thumbBuff.append("http://" + Constant.SERVER_NAME + ":" + request.getServerPort() + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + dir + System.getProperty("file.separator") + thumb + ",");
	                            }
	                            String thumbString = thumbBuff.substring(0, thumbBuff.length() - 1);
	                            //设置缩略图即可
	                            retMap.put("thumbUrl", thumbString);
	                        }*/
	                        return new ReturnData(retMap,200,"请求成功");
	                    }
	                }else {
	                    return new ReturnData(701,"未发现图片");
	                }
	            }else{
	                return new ReturnData(700,"未发现图片");
	            }
	        }catch (Exception e){
	           return new ReturnData(400,"参数有误");
	        }
	    }

	        @ResponseBody//版本文件上传
		    @RequestMapping(value = "/pCEditionUpload",method = RequestMethod.POST)
		    public Object pCEditionUpload(String type, String needsThumb, HttpServletRequest request)throws Exception{
		        try {
		            String  dir= "ZYCXAPKFILE";         
		            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		            StringBuffer pictureUrls =new StringBuffer();
		            List<String> imagePath = new ArrayList<>();
		            if (multipartResolver.isMultipart(request)) {
		                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
		                Iterator<String> iter = multiRequest.getFileNames();
		                if(iter.hasNext()){
		                    while (iter.hasNext()) {
		                        MultipartFile file = multiRequest.getFile((String) iter.next());
		                        //文件名称
		                        String fileName = file.getOriginalFilename();
		                        if (!fileName.equals("")) {
										/*String path1 = Thread.currentThread()
												.getContextClassLoader().getResource("").getPath() + "dowload" + File.separator;*/
		                            //获得文件后缀名称
		                            String prefix =fileName.substring(fileName.lastIndexOf(".")+1);
		                            //图片文件放的位置文件夹目录
		                            String path = "";
		                           /* if(prefix.equalsIgnoreCase("png") || prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("bmp")){*/
		                                // 项目在容器中实际发布运行的根路径
		                                String realPath =  request.getSession().getServletContext().getRealPath("/");
		                                String path2 = realPath.substring(0,realPath.lastIndexOf(System.getProperty("file.separator")));
		                                String p = realPath.substring(0, (path2.lastIndexOf(System.getProperty("file.separator"))))+System.getProperty("file.separator")+"zycxFile"+System.getProperty("file.separator")+dir;
		                                // 自定义的文件名称
		                                String trueFileName= UUIDUtil.getOrderSnByTime18()+"."+prefix;
		                                // 设置存放图片文件的路径
		                                path=p+System.getProperty("file.separator")+trueFileName;
		                                //转为标准文件盘符格式
		                                String acesP = "http://"+ Constant.SERVER_NAME+":"+request.getServerPort()+"//"+"zycxFile"+"//"+dir+"//"+trueFileName;
		                                final File localFile = new File(path);
		                                //将文件复制到指定文件夹下面
		                                try {
		                                    if(!localFile.exists()){
		                                        localFile.mkdirs();
		                                    }
		                                    file.transferTo(localFile);
		                                } catch (Exception e) {
		                                }
		                                imagePath.add(path);
		                                StringBuffer sb = new StringBuffer();
		                                sb.append(acesP);
		                                sb.append(",");
		                                pictureUrls.append(sb);
		                            /*}else{
		                                return new ReturnData(702,"系统暂不支持此格式图片");
		                            }*/
		                        }
		                    }
		                    String purl = "";
		                    if(StringTools.isEmpty(pictureUrls.toString())){
		                        return new ReturnData(703,"没有图片路径");
		                    }else {
		                        Map<String,String> retMap =new HashMap<>();
		                        purl = pictureUrls.substring(0, pictureUrls.length() - 1);
		                        retMap.put("purl", purl);
		        /*                    
		                            是否要压缩图片
		                            
		                        if(StringTools.numIsGT0(needsThumb)&&Integer.parseInt(needsThumb)==1) {
		                            StringBuffer thumbBuff = new StringBuffer();
		                            for (String img : imagePath) {
		                                String thumb = ImageCompressUtil.saveMinPhoto(img, Constant.IMAGECOMPRESS_SMALLICON, Constant.IMAGECOMPRESS_COMBASE, Constant.IMAGECOMPRESS_SCALE);
		                                thumb = thumb.substring(thumb.lastIndexOf(System.getProperty("file.separator")));
		                                thumbBuff.append("http://" + Constant.SERVER_NAME + ":" + request.getServerPort() + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + dir + System.getProperty("file.separator") + thumb + ",");
		                            }
		                            String thumbString = thumbBuff.substring(0, thumbBuff.length() - 1);
		                            //设置缩略图即可
		                            retMap.put("thumbUrl", thumbString);
		                        }*/
		                        return new ReturnData(retMap,200,"请求成功");
		                    }
		                }else {
		                    return new ReturnData(701,"未发现图片");
		                }
		            }else{
		                return new ReturnData(700,"未发现图片");
		            }
		        }catch (Exception e){
		           return new ReturnData(400,"参数有误");
		        }
		    }
}
